<?php
require("vs-top.dat");
?>

<div class="content">

<?php
	$vs = explode("\n", $_POST["vss"]);
	$nicky=array();
	if(count($vs)>1) {
		$ok = true;
		for($i=0; $i<count($vs); $i++) {
			$vs[$i]=chop($vs[$i]);
			//echo("SELECT nick FROM ucast WHERE u_vs='".$vs[$i]."'");
			$result = mysql_query("SELECT nick FROM ucast WHERE u_vs='".$vs[$i]."'") or die(mysql_error());
			if(mysql_num_rows($result)!=0) {
				$row=mysql_fetch_assoc($result);
				echo("vs ".$vs[$i]." ma nick ".$row["nick"].".<br />");
				array_push($nicky, $row["nick"]);
			} else {
				echo("<b>vs ".$vs[$i]." nebyl nalezen!</b><br />");
				$ok=false;
			}
		}
		if($ok) {
			mysql_query("INSERT INTO vs (u_vs, new_vs) VALUES ('".$vs[0]."', '-1')") or die(mysql_error());
			$result2 = mysql_query("SELECT vs_id FROM vs WHERE new_vs='-1'") or die(mysql_error());
			$row2=mysql_fetch_assoc($result2);
			$vs_id = $row2["vs_id"];
			$new_vs =  $vs_id*13;
			mysql_query("UPDATE vs SET new_vs='".$new_vs."' WHERE vs_id=".$vs_id) or die(mysql_error());
			for($i=1; $i<count($vs); $i++) {
				mysql_query("INSERT INTO vs (u_vs, new_vs) VALUES ('".$vs[$i]."', '".$new_vs."')") or die(mysql_error());
			}
			echo("Spolecny vs pro vyse uvedene je <b>".$new_vs."</b><br />");
			$mail_text = "Variabilni symbol prideleny ucastnikum ".$nick[0].", ";
			for($i=1; $i<count($nicky)-1; $i++) {
				$mail_text .= $nicky[$i].", ";
			}
			$mail_text .= $nicky[count($nicky)-1]." je: ".$new_vs;
			mail($_POST["mail"], "Hromadny variabilni symbol pro zaplaceni pp2007", $mail_text);
		} else {
			echo("<a href=\"multiple_vs\">zpet</a><br />");
		}
	} else {
		echo("musi byt zadany alespon 2 vs (na kazdem radku jeden)<br />");
		echo("<a href=\"multiple_vs\">zpet</a><br />");
	}
?>

</div>

<?php
require("vs-end.dat");
?>